package com.csp.mingyue.common.email.config;

import cn.hutool.extra.mail.MailAccount;
import com.csp.mingyue.common.email.support.EmailProperties;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;

/**
 * Java Mail 配置类
 *
 * @author Strive
 * @date 2023/9/1 17:31
 */
@AutoConfiguration
@EnableConfigurationProperties(EmailProperties.class)
public class EmailConfiguration {

	@Bean
	@ConditionalOnProperty(value = "email.enabled", havingValue = "true")
	public MailAccount mailAccount(EmailProperties emailProperties) {
		MailAccount account = new MailAccount();
		account.setHost(emailProperties.getHost());
		account.setPort(emailProperties.getPort());
		account.setAuth(emailProperties.getAuth());
		account.setFrom(emailProperties.getFrom());
		account.setUser(emailProperties.getUser());
		account.setPass(emailProperties.getPass());
		account.setSocketFactoryPort(emailProperties.getPort());
		account.setStarttlsEnable(emailProperties.getStarttlsEnable());
		account.setSslEnable(emailProperties.getSslEnable());
		account.setTimeout(emailProperties.getTimeout());
		account.setConnectionTimeout(emailProperties.getConnectionTimeout());
		return account;
	}

}
